package org.clockworkmages.games.anno1186;

import org.clockworkmages.games.anno1186.model.map.tactical.GameArea;
import org.clockworkmages.games.anno1186.model.map.tactical.GameAreaDO;
import org.clockworkmages.games.anno1186.model.map.tactical.Tile;
import org.clockworkmages.games.anno1186.model.map.tactical.TileObject;

public class GameAreaService {

	@Injected
	private GameDataService gameDataService;

	public GameArea fromDO(GameAreaDO gameAreaDO) {
		GameArea gameArea = new GameArea();

		Tile[][] tiles = new Tile[gameAreaDO.getX()][gameAreaDO.getY()];
		gameArea.setTiles(tiles);
		int i = 0;
		for (int y = gameAreaDO.getY() - 1; y >= 0; y--) {
			for (int x = 0; x < gameAreaDO.getX(); x++) {
				String tileObjectsString = gameAreaDO.getTiles().get(i++);
				Tile tile = new Tile();
				for (String tileObjectId : tileObjectsString.split(" ")) {
					TileObject tileObject = gameDataService.getGameData()
							.getTileObjects().get(tileObjectId);
					tile.getTileObjects().add(tileObject);
				}
				tiles[x][y] = tile;
			}
		}
		return gameArea;
	}

}
